<template>
  <div id="register">
    <div class="register-wrapper">
        <div class="logo">
          <img width="150px" src="../../../public/images/logo.jpg" alt="">
          <span class="banktitle">蜗牛银行管理系统</span>
        </div>
        <div class="wrapper">
          <title>注册</title>
          <el-form :model="user" status-icon :rules="rules" ref="user" label-width="100px" class="demo-user">
            <el-form-item label="用户名" prop="username">
              <el-input v-model.number="user.username" @blur="isExist"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
              <el-input type="password" v-model="user.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input type="password" v-model="user.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('user')" :plain="true">提交</el-button>
              <el-button @click="resetForm('user')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
    </div>

  </div>
</template>

<script>
  import axios from 'axios'
  import apis from '../../http/apis/apis'
  export default {
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年龄不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value < 18) {
              callback(new Error('必须年满18岁'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.user.checkPass !== '') {
            this.$refs.user.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.user.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        user: {
          pass: '',
          checkPass: '',
          username: ''
        },
        isUsername: '',
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          age: [
            { validator: checkAge, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.isUsername) {
              this.register();
            }
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      async isExist() {
        const {data} = await apis.users.isExist({username: this.user.username})
        if(data.status) {
          this.isUsername = 1
          console.log(data);
        }else {
          this.isUsername = 0
        }
      },
      async register() {
        const {data} = await apis.users.register({username: this.user.username, password: this.user.pass})
        if(data.status) {
          this.open2()
        }

      },
      open2() {
        this.$message({
          message: '注册成功',
          type: 'success'
        });
      },
    } 
  }

</script>

<style lang="scss">
  #register {
    height: 100%;
    background: linear-gradient(to right, #2A85ED,#36ECE1); 
    

    .register-wrapper{
      width: 700px;
      height: 350px;
      background-color: #fff;
      border-radius: 10px;
      overflow: hidden;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      display: flex;
      align-content: center;
      justify-content: center;
      padding: 0 20px;
      .logo {
        display: flex;
        flex-direction: column;
        align-content: center;
        justify-content: center;
        img {
          box-shadow: 0px 0px 5px #ccc;
          border-radius: 50%;
        }
        .banktitle {
          font-size: 20px;
          color: #878E9D;
          margin-top: 20px;
        }
      }
      .wrapper {
        margin-left: 60px;
        width: 300px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        .btn {
          display: flex;
          justify-content: flex-end;
          align-content: flex-end;
          button {
            margin-left: 30px;
            border-radius: 5px;
            border: none;
            outline: none;
            width: 150px;
            height: 40px;
            border: 1px solid #ccc;
            background: linear-gradient(to right, #2A85ED,#36ECE1); 
            color: #fff;
            cursor: pointer;
          }
          span {
            font-size: 12px;
            cursor: pointer;
          }
          
        }
      }
    }
  }
</style>